package link_list;

public class DoubleLikeListDemo {
    public static void main(String[] args) {
        Node n1 = new Node(2,"邓一","小邓");
        Node n2 = new Node(1,"赵六","小六");
        Node n3 = new Node(3,"张三","小三");
        Node n4 = new Node(4,"李四","小四");
        Node n5 = new Node(3,"王五","小五");
        DoubleList list = new DoubleList();
        list.addNo(n1);
        list.addNo(n2);
        list.addNo(n3);
        list.addNo(n4);
        list.addNo(n5);
//        list.add(n1);
//        list.add(n2);
//        list.add(n3);
//        list.add(n4);
//        list.add(n5);
        list.show();
//        System.out.println("================");
//        list.delete(3);
//        list.show();
//        list.update(new Node(3,"六一","小一"));
//        System.out.println("----------------");
//        list.show();
    }
}
class DoubleList {
    private final Node headNode = new Node(0,"","");
    //添加
    public void add(Node node) {
        if(node.getNo() < 0) {
            System.out.println("node的no必须大于0的整数");
            return;
        }
        Node temp = headNode;
        boolean flag = false;
        while (true) {
            if(temp.getNo() == node.getNo()) {
                flag = true;
                break;
            }
            if(temp.next == null) {
                temp.next = node;
                node.pre = temp;
                break;
            }
            temp = temp.next;
        }
        if(flag) {
            System.out.println("加入数据的" + node.getNo() + "存在,添加变为修改");
            update(node);
        }
    }
    //按顺序添加
    public void addNo(Node node) {
        if(node.getNo() < 0) {
            System.out.println("node的no必须大于0的整数");
            return;
        }
        Node temp = headNode;
        boolean flag = false;
        while (true) {
            if(temp.getNo() > node.getNo()) {
                flag = true;
                break;
            }else if(temp.getNo() == node.getNo()) {
                System.out.println("加入数据的"+node.getNo()+"存在,添加变为修改");
                update(node);
                break;
            }
            if(temp.next == null) {
                temp.next = node;
                node.pre = temp;
                break;
            }
            temp = temp.next;
        }
        if(flag) {
            node.next = temp;
            node.pre =temp.pre;
            temp.pre.next = node;
            temp.pre = node;
        }
    }
    //删除
    public void delete(int no) {
        Node temp = headNode.next;
        boolean flag = false;
        if(temp == null) {
            System.out.println("链表为空");
            return;
        }
        while (temp != null) {
            if(temp.getNo() == no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if(flag) {
            temp.pre.next = temp.next;
            temp.next.pre = temp.pre;
        }else {
            System.out.println("没有找到要删除的数据");
        }
    }
    //修改
    public void update(Node node) {
        Node temp = headNode.next;
        boolean flag = false;
        if(temp == null) {
            System.out.println("链表为空");
            return;
        }
        while (temp != null) {
            if(temp.getNo() == node.getNo()) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if(flag) {
            temp.pre.next = node;
            node.pre = temp.pre;
            if(temp.next != null) {
                temp.next.pre = node;
                node.next = temp.next;
            }
        }else {
            System.out.println("没有找到要修改的数据");
        }
    }
    public void show() {
        Node temp = headNode.next;
        if(temp == null) {
            System.out.println("链表为空");
            return;
        }
        while (temp != null) {
            System.out.println(temp);
            temp = temp.next;
        }
    }
}
class Node {
    private int no;
    private String name;
    private String nickName;
    public Node next;
    public Node pre;

    public Node(int no, String name, String nickName) {
        this.no = no;
        this.name = name;
        this.nickName = nickName;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    @Override
    public String toString() {
        return "Node{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickName='" + nickName + '\'' +
                '}';
    }
}
